﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinServiceFileListener.Helpers
{
    public class FolderHelper
    {
        /// <summary>
        /// 在该路径下以时间格式创建（不重复的）文件夹
        /// </summary>
        /// <param name="folderPath">文件目录</param>
        /// <param name="folderFormat">创建文件夹的格式</param>
        /// <returns></returns>
        public static string CreateDateFolder(string folderPath, string folderFormat = "yyyy-MMdd HHmm")
        {
            int count = 1;
            string baseDirc = DateTime.Now.ToString(folderFormat);
            string newDirc = baseDirc;
            string newPath = Path.Combine(folderPath, newDirc);
            while (Directory.Exists(newPath))
            {
                newDirc = $"{baseDirc}（{count}）";
                newPath = Path.Combine(folderPath, newDirc);
                count++;
            }
            return newPath;
        }
        /// <summary>
        /// 获取上级目录
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <returns></returns>
        public static string GetParentDirectory(string filePath)
        {
            // 获取文件的目录
            string directory = Path.GetDirectoryName(filePath);

            if (directory != null) // 确保目录不为 null
            {
                // 获取上级目录
                DirectoryInfo parentDirInfo = Directory.GetParent(directory);
                return parentDirInfo?.FullName; // 返回上级目录的完整路径
            }

            return null; // 如果无法找到上级目录则返回 null
        }
        /// <summary>
        /// 如果文件夹不存在，则创建
        /// </summary>
        /// <param name="folder">文件夹目录</param>
        public static void CreateFolder(string folder)
        {
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
        }
    }
}
